package com.shike.hq.controller;

import com.shike.hq.entity.Admin;
import com.shike.hq.entity.Area;
import com.shike.hq.security.AdminUserDetailsService;
import com.shike.hq.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;

/**
 * The type Area controller.
 *
 * @author Saw96x
 * @date 21 /05/03 - 09:05
 */
@Controller
@RequestMapping("/base")
public class AreaController {

  private static final String REDIRECT = "redirect:/base/area";

  @Autowired
  private AreaService areaService;


  /**
   * 区域页面路径映射
   *
   * @param session session类型
   * @param model   model类型
   * @return 页面路径字符串
   */
  @GetMapping("/area")
  public String area(Model model, HttpSession session) {
    Admin admin = (Admin) session.getAttribute("admin");
    long areaId = admin.getAreaId();
    ArrayList<Area> areas = areaService.selectAllAreaById(areaId);
    if (areas.size() > 0) {
      //找到区域信息
      model.addAttribute("areas", areas);
    } else {
      model.addAttribute("areas", "null");
    }
    return "base/area";
  }

//  利用principal获取信息
//  @GetMapping("/area")
//  public String area(Model model){
//    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//    System.out.println(principal);
//    Admin adminUser = (Admin) principal;
//    long areaId = adminUser.getAreaId();
//    ArrayList<Area> areas = areaService.selectAllAreaById(areaId);
//    if (areas.size() > 0) {
//      //找到区域信息
//      model.addAttribute("areas", areas);
//    } else {
//      model.addAttribute("areas", "null");
//    }
//    return "base/area";
//  }

  /**
   * 编辑区域信息
   *
   * @param area 编辑后的区域信息
   * @param a    model类型
   * @return 页面路径字符串
   */
//编辑区域信息
  @PostMapping("/area/update")
  public String updateArea(Area area, RedirectAttributes attributes, HttpSession session) {
    Admin admin = (Admin) session.getAttribute("admin");
    area.setParent(admin.getAreaId());
    try {
      areaService.updateById(area);
      attributes.addFlashAttribute("msg", "编辑成功！");
    } catch (Exception e) {
      attributes.addFlashAttribute("msg", "编辑失败！");
    }
    return REDIRECT;
  }

  /**
   * 删除区域信息
   *
   * @param id         要删除的区域id
   * @param attributes model类型
   * @return 页面路径字符串
   */
//删除单条区域信息
  @GetMapping("/area/delete")
  public String deleteArea(@RequestParam("id") long id, RedirectAttributes attributes) {
    try {
      areaService.removeById(id);
      attributes.addFlashAttribute("msg", "删除成功！");
    } catch (Exception e) {
      attributes.addFlashAttribute("msg", "删除失败！该区域下仍有相关信息！");
    }
    return REDIRECT;
  }

  /**
   * 删除多条区域信息
   *
   * @param idList     区域id列表
   * @param attributes model类型
   * @return 页面路径字符串
   */
  @GetMapping("/area/deleteAreas")
  public String deleteAreas(@RequestParam("idList") ArrayList<Long> idList, RedirectAttributes attributes) {
    try {
      areaService.removeByIds(idList);
      attributes.addFlashAttribute("msg", "删除成功！");
    } catch (Exception e) {
      attributes.addFlashAttribute("msg", "删除失败！部分区域下仍有相关信息！");
    }

    return REDIRECT;
  }

  /**
   * 添加区域信息
   *
   * @param area       添加的区域信息
   * @param attributes model类型
   * @param session    session对象
   * @return 页面路径字符串
   */
  @PostMapping("/area/add")
  public String addArea(Area area, RedirectAttributes attributes, HttpSession session) {
    Admin admin = (Admin) session.getAttribute("admin");
    area.setParent(admin.getAreaId());
    area.setCreateTime(new Timestamp(System.currentTimeMillis()));
    try {
      areaService.save(area);
      attributes.addFlashAttribute("msg", "添加成功！");
    } catch (Exception e) {
      attributes.addFlashAttribute("msg", "添加失败！");
    }
    return REDIRECT;
  }
}
